home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Services / Weather / Common.php next >
Encoding:
PHP Script  |  2004-10-01  |  17.5 KB  |  589 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at                              |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Alexander Wirtz <alex@pc4p.net>                             |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Common.php,v 1.36 2004/05/11 18:56:55 eru Exp $
  20.  
  21. /**
  22. * @package      Services_Weather
  23. * @filesource
  24. */
  25.  
  26. /**
  27. */
  28. require_once "Services/Weather.php";
  29.  
  30. // {{{ constants
  31. // {{{ natural constants and measures
  32. define("SERVICES_WEATHER_RADIUS_EARTH", 6378.15);
  33. // }}}
  34. // }}}
  35.  
  36. // {{{ class Services_Weather_Common
  37. /**
  38. * PEAR::Services_Weather_Common
  39. *
  40. * Parent class for weather-services. Defines common functions for unit
  41. * conversions, checks for cache enabling and does other miscellaneous
  42. * things. 
  43. *
  44. * @author       Alexander Wirtz <alex@pc4p.net>
  45. * @package      Services_Weather
  46. * @license      http://www.php.net/license/2_02.txt
  47. * @version      1.3
  48. */
  49. class Services_Weather_Common {
  50.  
  51.     // {{{ properties
  52.     /**
  53.     * Format of the units provided (standard/metric/custom)
  54.     *
  55.     * @var      string                      $_unitsFormat
  56.     * @access   private
  57.     */
  58.     var $_unitsFormat = "s";
  59.  
  60.     /**
  61.     * Custom format of the units
  62.     *
  63.     * @var      array                       $_customUnitsFormat
  64.     * @access   private
  65.     */
  66.     var $_customUnitsFormat = array(
  67.         "temp"   => "f",
  68.         "vis"    => "sm",
  69.         "height" => "ft",
  70.         "wind"   => "mph",
  71.         "pres"   => "in",
  72.         "rain"   => "in"
  73.     );
  74.  
  75.     /**
  76.     * Timeout for HTTP requests
  77.     *
  78.     * @var        int                            $_httpTimeout
  79.     * @access    private
  80.     */
  81.     var $_httpTimeout = 60;
  82.  
  83.     /**
  84.     * Format of the used dates
  85.     *
  86.     * @var      string                      $_dateFormat
  87.     * @access   private
  88.     */
  89.     var $_dateFormat = "m/d/y";
  90.  
  91.     /**
  92.     * Format of the used times
  93.     *
  94.     * @var      string                      $_timeFormat
  95.     * @access   private
  96.     */
  97.     var $_timeFormat = "G:i A";
  98.  
  99.     /**
  100.     * Object containing the location-data
  101.     *
  102.     * @var      object stdClass             $_location
  103.     * @access   private
  104.     */
  105.     var $_location;
  106.  
  107.     /**
  108.     * Object containing the weather-data
  109.     *
  110.     * @var      object stdClass             $_weather
  111.     * @access   private
  112.     */
  113.     var $_weather;
  114.  
  115.     /**
  116.     * Object containing the forecast-data
  117.     *
  118.     * @var      object stdClass             $_forecast
  119.     * @access   private
  120.     */
  121.     var $_forecast;
  122.  
  123.     /**
  124.     * Cache, containing the data-objects
  125.     *
  126.     * @var      object Cache                $_cache
  127.     * @access   private
  128.     */
  129.     var $_cache;
  130.  
  131.     /**
  132.     * Provides check for Cache
  133.     *
  134.     * @var      bool                        $_cacheEnabled
  135.     * @access   private
  136.     */
  137.     var $_cacheEnabled = false;
  138.     // }}}
  139.  
  140.     // {{{ constructor
  141.     /**
  142.     * Constructor
  143.     *
  144.     * @param    array                       $options
  145.     * @param    mixed                       $error
  146.     * @throws   PEAR_Error
  147.     * @see      Science_Weather::Science_Weather
  148.     * @access   private
  149.     */
  150.     function Services_Weather_Common($options, &$error)
  151.     {
  152.         // Set options accordingly        
  153.         if (isset($options["cacheType"])) {
  154.             if (isset($options["cacheOptions"])) {
  155.                 $status = $this->setCache($options["cacheType"], $options["cacheOptions"]);
  156.             } else {
  157.                 $status = $this->setCache($options["cacheType"]);
  158.             }
  159.             if (Services_Weather::isError($status)) {
  160.                 $error = $status;
  161.                 return;
  162.             }
  163.         }
  164.  
  165.         if (isset($options["unitsFormat"])) {
  166.             if (isset($options["customUnitsFormat"])) {
  167.                 $this->setUnitsFormat($options["unitsFormat"], $options["customUnitsFormat"]);
  168.             } else {
  169.                 $this->setUnitsFormat($options["unitsFormat"]);
  170.             }
  171.         }
  172.  
  173.         if (isset($options["httpTimeout"])) {
  174.             $this->setHttpTimeout($options["httpTimeout"]);
  175.         }
  176.         
  177.         if (isset($options["dateFormat"])) {
  178.             $this->setDateTimeFormat($options["dateFormat"], "");
  179.         }
  180.         if (isset($options["timeFormat"])) {
  181.             $this->setDateTimeFormat("", $options["timeFormat"]);
  182.         }
  183.     }
  184.     // }}}
  185.  
  186.     // {{{ setCache()
  187.     /**
  188.     * Enables caching the data, usage strongly recommended
  189.     *
  190.     * Requires Cache to be installed
  191.     *
  192.     * @param    string                      $cacheType
  193.     * @param    array                       $cacheOptions
  194.     * @return   PEAR_Error|bool
  195.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED
  196.     * @access   public
  197.     */
  198.     function setCache($cacheType = "file", $cacheOptions = array())
  199.     {
  200.         // The error handling in Cache is a bit crummy (read: not existent)
  201.         // so we have to do that on our own...
  202.         @include_once "Cache.php";
  203.         @$cache = new Cache($cacheType, $cacheOptions);
  204.         if (is_object($cache) && (strtolower(get_class($cache)) == "cache" || is_subclass_of($cache, "cache"))) {
  205.             $this->_cache        = $cache;
  206.             $this->_cacheEnabled = true;
  207.         } else {
  208.             $this->_cache        = null;
  209.             $this->_cacheEnabled = false;
  210.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_CACHE_INIT_FAILED, __FILE__, __LINE__);
  211.         }
  212.  
  213.         return true;
  214.     }
  215.     // }}}
  216.  
  217.     // {{{ setUnitsFormat()
  218.     /**
  219.     * Changes the representation of the units (standard/metric)
  220.     *
  221.     * @param    string                      $unitsFormat
  222.     * @param    array                       $customUnitsFormat
  223.     * @access   public
  224.     */
  225.     function setUnitsFormat($unitsFormat, $customUnitsFormat = array())
  226.     {
  227.         static $acceptedFormats;
  228.         if (!isset($acceptedFormats)) {
  229.             $acceptedFormats = array(
  230.                 "temp"   => array("c", "f"),
  231.                 "vis"    => array("m", "km", "ft", "sm"),
  232.                 "height" => array("m", "ft"),
  233.                 "wind"   => array("mph", "kmh", "kt", "mps", "fps"),
  234.                 "pres"   => array("in", "hpa", "mb", "mm", "atm"),
  235.                 "rain"   => array("in", "mm")
  236.             );
  237.         }
  238.         
  239.         if (strlen($unitsFormat) && in_array(strtolower($unitsFormat{0}), array("c", "m", "s"))) {
  240.             $this->_unitsFormat = strtolower($unitsFormat{0});
  241.             if ($this->_unitsFormat == "c" && is_array($customUnitsFormat)) {
  242.                 foreach ($customUnitsFormat as $key => $value) {
  243.                     if (array_key_exists($key, $acceptedFormats) && in_array($value, $acceptedFormats[$key])) {
  244.                         $this->_customUnitsFormat[$key] = $value;
  245.                     }
  246.                 }
  247.             } elseif ($this->_unitsFormat == "c") {
  248.                 $this->_unitsFormat = "s";
  249.             }
  250.         }
  251.     }
  252.     // }}}
  253.  
  254.     // {{{ setHttpTimeout()
  255.     /**
  256.     * Sets the timeout in seconds for HTTP requests
  257.     *
  258.     * @param    int                          $httpTimeout
  259.     * @access   public
  260.     */
  261.     function setHttpTimeout($httpTimeout)
  262.     {
  263.         if (is_int($httpTimeout)) {
  264.             $this->_httpTimeout = $httpTimeout;
  265.         }
  266.     }
  267.     // }}}
  268.  
  269.     // {{{ getUnitsFormat()
  270.     /**
  271.     * Returns the selected units format
  272.     *
  273.     * @param    string                      $unitsFormat
  274.     * @return   array
  275.     * @access   public
  276.     */
  277.     function getUnitsFormat($unitsFormat = "")
  278.     {
  279.         // This is cheap'o stuff
  280.         if (strlen($unitsFormat) && in_array(strtolower($unitsFormat{0}), array("c", "m", "s"))) {
  281.             $unitsFormat = strtolower($unitsFormat{0});
  282.         } else {
  283.             $unitsFormat = $this->_unitsFormat;
  284.         }
  285.  
  286.         $c = $this->_customUnitsFormat;
  287.         $m = array(
  288.             "temp"   => "c",
  289.             "vis"    => "km",
  290.             "height" => "m",
  291.             "wind"   => "kmh",
  292.             "pres"   => "mb",
  293.             "rain"   => "mm"
  294.         );
  295.         $s = array(
  296.             "temp"   => "f",
  297.             "vis"    => "sm",
  298.             "height" => "ft",
  299.             "wind"   => "mph",
  300.             "pres"   => "in",
  301.             "rain"   => "in"
  302.         );
  303.  
  304.         return ${$unitsFormat};
  305.     }
  306.     // }}}
  307.  
  308.     // {{{ setDateTimeFormat()
  309.     /**
  310.     * Changes the representation of time and dates (see http://www.php.net/date)
  311.     *
  312.     * @param    string                      $dateFormat
  313.     * @param    string                      $timeFormat
  314.     * @access   public
  315.     */
  316.     function setDateTimeFormat($dateFormat = "", $timeFormat = "")
  317.     {
  318.         if (strlen($dateFormat)) {
  319.             $this->_dateFormat = $dateFormat;
  320.         }
  321.         if (strlen($timeFormat)) {
  322.             $this->_timeFormat = $timeFormat;
  323.         }
  324.     }
  325.     // }}}
  326.  
  327.     // {{{ convertTemperature()
  328.     /**
  329.     * Convert temperature between f and c
  330.     *
  331.     * @param    float                       $temperature
  332.     * @param    string                      $from
  333.     * @param    string                      $to
  334.     * @return   float
  335.     * @access   public
  336.     */
  337.     function convertTemperature($temperature, $from, $to)
  338.     {
  339.         $from = strtolower($from{0});
  340.         $to   = strtolower($to{0});
  341.  
  342.         $result = array(
  343.             "f" => array(
  344.                 "f" => $temperature,            "c" => ($temperature - 32) / 1.8
  345.             ),
  346.             "c" => array(
  347.                 "f" => 1.8 * $temperature + 32, "c" => $temperature
  348.             )
  349.         );
  350.  
  351.         return round($result[$from][$to], 2);
  352.     }
  353.     // }}}
  354.  
  355.     // {{{ convertSpeed()
  356.     /**
  357.     * Convert speed between mph, kmh, kt, mps and fps
  358.     *
  359.     * @param    float                       $speed
  360.     * @param    string                      $from
  361.     * @param    string                      $to
  362.     * @return   float
  363.     * @access   public
  364.     */
  365.     function convertSpeed($speed, $from, $to)
  366.     {
  367.         $from = strtolower($from);
  368.         $to   = strtolower($to);
  369.  
  370.         static $factor;
  371.         if (!isset($factor)) {
  372.             $factor = array(
  373.                 "mph" => array(
  374.                     "mph" => 1,         "kmh" => 1.609344, "kt" => 0.8689762, "mps" => 0.44704,   "fps" => 1.4666667
  375.                 ),
  376.                 "kmh" => array(
  377.                     "mph" => 0.6213712, "kmh" => 1,        "kt" => 0.5399568, "mps" => 0.2777778, "fps" => 0.9113444
  378.                 ),
  379.                 "kt"  => array(
  380.                     "mph" => 1.1507794, "kmh" => 1.852,    "kt" => 1,         "mps" => 0.5144444, "fps" => 1.6878099
  381.                 ),
  382.                 "mps" => array(
  383.                     "mph" => 2.2369363, "kmh" => 3.6,      "kt" => 1.9438445, "mps" => 1,         "fps" => 3.2808399
  384.                 ),
  385.                 "fps" => array(
  386.                     "mph" => 0.6818182, "kmh" => 1.09728,  "kt" => 0.5924838, "mps" => 0.3048,    "fps" => 1
  387.                 )
  388.             );
  389.         }
  390.  
  391.         return round($speed * $factor[$from][$to], 2);
  392.     }
  393.     // }}}
  394.  
  395.     // {{{ convertPressure()
  396.     /**
  397.     * Convert pressure between in, hpa, mb, mm and atm
  398.     *
  399.     * @param    float                       $pressure
  400.     * @param    string                      $from
  401.     * @param    string                      $to
  402.     * @return   float
  403.     * @access   public
  404.     */
  405.     function convertPressure($pressure, $from, $to)
  406.     {
  407.         $from = strtolower($from);
  408.         $to   = strtolower($to);
  409.  
  410.         static $factor;
  411.         if (!isset($factor)) {
  412.             $factor = array(
  413.                 "in"   => array(
  414.                     "in" => 1,         "hpa" => 33.863887, "mb" => 33.863887, "mm" => 25.4,      "atm" => 0.0334213
  415.                 ),
  416.                 "hpa"  => array(
  417.                     "in" => 0.02953,   "hpa" => 1,         "mb" => 1,         "mm" => 0.7500616, "atm" => 0.0009869
  418.                 ),
  419.                 "mb"   => array(
  420.                     "in" => 0.02953,   "hpa" => 1,         "mb" => 1,         "mm" => 0.7500616, "atm" => 0.0009869
  421.                 ),
  422.                 "mm"   => array(
  423.                     "in" => 0.0393701, "hpa" => 1.3332239, "mb" => 1.3332239, "mm" => 1,         "atm" => 0.0013158
  424.                 ),
  425.                 "atm"  => array(
  426.                     "in" => 29,921258, "hpa" => 1013.2501, "mb" => 1013.2501, "mm" => 759.999952, "atm" => 1
  427.                 )
  428.             );
  429.         }
  430.  
  431.         return round($pressure * $factor[$from][$to], 2);
  432.     }
  433.     // }}}
  434.  
  435.     // {{{ convertDistance()
  436.     /**
  437.     * Convert distance between km, ft and sm
  438.     *
  439.     * @param    float                       $distance
  440.     * @param    string                      $from
  441.     * @param    string                      $to
  442.     * @return   float
  443.     * @access   public
  444.     */
  445.     function convertDistance($distance, $from, $to)
  446.     {
  447.         $to   = strtolower($to);
  448.         $from = strtolower($from);
  449.  
  450.         static $factor;
  451.         if (!isset($factor)) {
  452.             $factor = array(
  453.                 "m" => array(
  454.                     "m" => 1,            "km" => 1000,      "ft" => 3.280839895, "sm" => 0.0006213699
  455.                 ),
  456.                 "km" => array(
  457.                     "m" => 0.001,        "km" => 1,         "ft" => 3280.839895, "sm" => 0.6213699
  458.                 ),
  459.                 "ft" => array(
  460.                     "m" => 0.3048,       "km" => 0.0003048, "ft" => 1,           "sm" => 0.0001894
  461.                 ),
  462.                 "sm" => array(
  463.                     "m" => 0.0016093472, "km" => 1.6093472, "ft" => 5280.0106,   "sm" => 1
  464.                 )
  465.             );
  466.         }
  467.  
  468.         return round($distance * $factor[$from][$to], 2);
  469.     }
  470.     // }}}
  471.  
  472.     // {{{ calculateWindChill()
  473.     /**
  474.     * Calculate windchill from temperature and windspeed (enhanced formula)
  475.     *
  476.     * Temperature has to be entered in deg F, speed in mph!
  477.     *
  478.     * @param    float                       $temperature
  479.     * @param    float                       $speed
  480.     * @return   float
  481.     * @access   public
  482.     * @link     http://www.nws.noaa.gov/om/windchill/      
  483.     */
  484.     function calculateWindChill($temperature, $speed)
  485.     {
  486.         return round(35.74 + 0.6215 * $temperature - 35.75 * pow($speed, 0.16) + 0.4275 * $temperature * pow($speed, 0.16));
  487.     }
  488.     // }}}
  489.  
  490.     // {{{ calculateHumidity()
  491.     /**
  492.     * Calculate humidity from temperature and dewpoint
  493.     * This is only an approximation, there is no exact formula, this
  494.     * one here is called Magnus-Formula
  495.     *
  496.     * Temperature and dewpoint have to be entered in deg C!
  497.     *
  498.     * @param    float                       $temperature
  499.     * @param    float                       $dewPoint
  500.     * @return   float
  501.     * @access   public
  502.     * @link     http://www.faqs.org/faqs/meteorology/temp-dewpoint/
  503.     */
  504.     function calculateHumidity($temperature, $dewPoint)
  505.     {   
  506.         // First calculate saturation steam pressure for both temperatures
  507.         if ($temperature >= 0) {
  508.             $a = 7.5;
  509.             $b = 237.3;
  510.         } else {
  511.             $a = 7.6;
  512.             $b = 240.7;
  513.         }
  514.         $tempSSP = 6.1078 * pow(10, ($a * $temperature) / ($b + $temperature));
  515.  
  516.         if ($dewPoint >= 0) {
  517.             $a = 7.5;
  518.             $b = 237.3;
  519.         } else {
  520.             $a = 7.6;
  521.             $b = 240.7;
  522.         }
  523.         $dewSSP  = 6.1078 * pow(10, ($a * $dewPoint) / ($b + $dewPoint));
  524.         
  525.         return round(100 * $dewSSP / $tempSSP, 1);
  526.     }
  527.     // }}}
  528.  
  529.     // {{{ calculateDewPoint()
  530.     /**
  531.     * Calculate dewpoint from temperature and humidity
  532.     * This is only an approximation, there is no exact formula, this
  533.     * one here is called Magnus-Formula
  534.     *
  535.     * Temperature has to be entered in deg C!
  536.     *
  537.     * @param    float                       $temperature
  538.     * @param    float                       $humidity
  539.     * @return   float
  540.     * @access   public
  541.     * @link     http://www.faqs.org/faqs/meteorology/temp-dewpoint/
  542.     */
  543.     function calculateDewPoint($temperature, $humidity)
  544.     {   
  545.         if ($temperature >= 0) {
  546.             $a = 7.5;
  547.             $b = 237.3;
  548.         } else {
  549.             $a = 7.6;
  550.             $b = 240.7;
  551.         }
  552.  
  553.         // First calculate saturation steam pressure for temperature
  554.         $SSP = 6.1078 * pow(10, ($a * $temperature) / ($b + $temperature));
  555.  
  556.         // Steam pressure
  557.         $SP  = $humidity / 100 * $SSP;
  558.  
  559.         $v   = log($SP / 6.1078, 10);
  560.  
  561.         return round($b * $v / ($a - $v), 1);
  562.     }
  563.     // }}}
  564.  
  565.     // {{{ polar2cartesian()
  566.     /**
  567.     * Convert polar coordinates to cartesian coordinates
  568.     *
  569.     * @param    float                       $latitude
  570.     * @param    float                       $longitude
  571.     * @return   array
  572.     * @access   public
  573.     */
  574.     function polar2cartesian($latitude, $longitude)
  575.     {
  576.         $theta = deg2rad($latitude);
  577.         $phi   = deg2rad($longitude);
  578.  
  579.         $x = SERVICES_WEATHER_RADIUS_EARTH * cos($phi) * cos($theta);
  580.         $y = SERVICES_WEATHER_RADIUS_EARTH * sin($phi) * cos($theta);
  581.         $z = SERVICES_WEATHER_RADIUS_EARTH             * sin($theta);
  582.  
  583.         return array($x, $y, $z);
  584.     }
  585.     // }}}
  586. }
  587. // }}}
  588. ?>
  589.